//------------------------------------------------------------------------------
//  <copyright file="PolygonTests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Test.Robotics.Geometry
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Geometry;
    using Microsoft.Robotics.Numerics;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Tests for Polygon class.
    /// </summary>
    [TestClass]
    public class PolygonTests
    {
        /// <summary>
        /// triangle points
        /// </summary>
        private Point2D[] trianglePoints = 
        {
            new Point2D(2, 4),
            new Point2D(5, 7),
            new Point2D(8, 3)
        };

        /// <summary>
        /// rectangle points
        /// </summary>
        private Point2D[] rectanglePoints =
        {
            new Point2D(-6, -2),
            new Point2D(-10, -6),
            new Point2D(-5, -11),
            new Point2D(-1, -7)
        };

        /// <summary>
        /// Test the constructor for Line2D
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void PolygonConstructorTest()
        {
            Polygon triangle = new Polygon(this.trianglePoints);
            Assert.AreEqual(3, triangle.NumPoints);

            Polygon rectangle = new Polygon(this.rectanglePoints);
            Assert.AreEqual(4, rectangle.NumPoints);
        }

        /// <summary>
        /// Test interior/exterior point discrimination
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void PolygonInteriorPointTest()
        {
            Polygon triangle = new Polygon(this.trianglePoints);
            Polygon rectangle = new Polygon(this.rectanglePoints);

            Point2D insideTriangle = new Point2D(5, 5);
            Point2D outsideTriangle = new Point2D(6, 7);

            Point2D insideRectangle = new Point2D(-7, -4);
            Point2D outsideRectangle = new Point2D(0, -3);

            Assert.IsTrue(rectangle.IsInterior(insideRectangle));
            Assert.IsTrue(triangle.IsInterior(insideTriangle));

            Assert.IsFalse(triangle.IsInterior(outsideTriangle));
            Assert.IsFalse(rectangle.IsInterior(outsideRectangle));
        }

        /// <summary>
        /// Test interior/exterior point discrimination
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void PolygonCircleIntersectionTest()
        {
            Polygon triangle = new Polygon(this.trianglePoints);
            Polygon rectangle = new Polygon(this.rectanglePoints);

            Circle interiorToTriangle = new Circle(new Point2D(5, 5), 0.3);
            Circle intersectingTriangle = new Circle(new Point2D(8, 4), 2);
            Circle nonIntersectingTriangle = new Circle(new Point2D(8, 6), 1);

            Circle interiorToRectangle = new Circle(new Point2D(-5, -6), 1);
            Circle intersectingRectangle = new Circle(new Point2D(-11, -5), 2);
            Circle nonIntersectingRectangle = new Circle(new Point2D(-2, -1), 1);

            Assert.IsTrue(triangle.Intersects(interiorToTriangle));
            Assert.IsTrue(triangle.Intersects(intersectingTriangle));
            Assert.IsFalse(triangle.Intersects(nonIntersectingTriangle));

            Assert.IsTrue(rectangle.Intersects(interiorToRectangle));
            Assert.IsTrue(rectangle.Intersects(intersectingRectangle));
            Assert.IsFalse(rectangle.Intersects(nonIntersectingRectangle));
        }

        /// <summary>
        /// Test that an exception is thrown if points passed to <c>Polygon</c>
        /// constructor are not unique.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(ArgumentException))]
        public void PolygonNonUniquePointsConstructorTest()
        {
            Polygon p = new Polygon(
            new Point2D[]
            {
                new Point2D(1, 2),
                new Point2D(3, 4),
                new Point2D(5, 6),
                new Point2D(3, 4)
            });
        }

        /// <summary>
        /// Test that an exception is thrown if points passed to <c>Polygon</c>
        /// constructor have intersecting edges
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(ArgumentException))]
        public void PolygonIntersectingEdgesConstructorTest()
        {
            Polygon p = new Polygon(
            new Point2D[]
            {
                new Point2D(2, 1),
                new Point2D(5, 4),
                new Point2D(5, 1),
                new Point2D(2, 4)
            });
        }
    }
}
